package com.zian.utils;

import java.util.Random;
import java.util.StringJoiner;

public class CaptchaUtil {
    public static String generate5CharCaptcha() {
        // 定义可选字符集，包括数字和大小写字母
        String chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";

        // 使用Random类生成随机索引
        Random random = new Random();

        // 初始化StringJoiner用于拼接验证码字符
        StringJoiner captchaJoiner = new StringJoiner("");

        // 遍历5次，每次从字符集中随机选取一个字符添加到验证码字符串中
        for (int i = 0; i < 5; i++) {
            int index = random.nextInt(chars.length());
            captchaJoiner.add(chars.charAt(index) + "");
        }

        // 返回生成的5位验证码字符串
        return captchaJoiner.toString();
    }




}
